package com.springboot_gradrecruit.service.impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.springboot_gradrecruit.common.PageResponse;
import com.springboot_gradrecruit.mapper.EnterpriseMapper;
import com.springboot_gradrecruit.mapper.JobMapper;
import com.springboot_gradrecruit.mapper.SendMapper;
import com.springboot_gradrecruit.model.dto.job.AddJobRequest;
import com.springboot_gradrecruit.model.dto.job.PageJobRequest;
import com.springboot_gradrecruit.model.dto.job.UpdateJobRequest;
import com.springboot_gradrecruit.model.entity.Enterprise;
import com.springboot_gradrecruit.model.entity.Job;
import com.springboot_gradrecruit.model.entity.Send;
import com.springboot_gradrecruit.model.vo.chart.CityAverageSalary;
import com.springboot_gradrecruit.model.vo.chart.IndustryJobSpread;
import com.springboot_gradrecruit.model.vo.job.JobVO;
import com.springboot_gradrecruit.service.JobService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
* @author zr
* @description 针对表【job】的数据库操作Service实现
* @createDate 2024-11-04 15:46:30
*/
@Service
public class JobServiceImpl extends ServiceImpl<JobMapper, Job>
    implements JobService {

    @Resource
    private JobMapper jobMapper;

    @Resource
    private EnterpriseMapper enterpriseMapper;

    @Resource
    private SendMapper sendMapper;

    // 添加岗位信息
    @Override
    public Long addJob(AddJobRequest addJobRequest, HttpServletRequest request) {
        //1.获取参数
        Job job = new Job();
        job.setJobCompanyId(addJobRequest.getJobCompanyId());
        job.setJobTitle(addJobRequest.getJobTitle());
        job.setJobLocation(addJobRequest.getJobLocation());
        job.setJobSalary(addJobRequest.getJobSalary());
        job.setJobEducationRequirement(addJobRequest.getJobEducationRequirement());
        job.setJobMajorRequirement(addJobRequest.getJobMajorRequirement());
        job.setJobExperienceRequirement(addJobRequest.getJobExperienceRequirement());
        job.setJobSkillRequirement(addJobRequest.getJobSkillRequirement());
        job.setJobDescription(addJobRequest.getJobDescription());
        job.setJobBenefits(addJobRequest.getJobBenefits());
        job.setJobPublishDate(addJobRequest.getJobPublishDate());
        job.setJobDeadline(addJobRequest.getJobDeadline());
        job.setJobContactInfo(addJobRequest.getJobContactInfo());
        job.setJobStatus(addJobRequest.getJobStatus());
        job.setJobIndustry(addJobRequest.getJobIndustry());

        //todo 添加相关企业信息
        //1.获取参数，查询对应的企业
        QueryWrapper<Enterprise> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("enterprise_id", addJobRequest.getJobCompanyId());
        Enterprise enterprise = enterpriseMapper.selectOne(queryWrapper);
        job.setJobCompanyName(enterprise.getEnterpriseName());
        job.setJobCompanySize(enterprise.getEnterpriseSize());
        job.setJobCompanyIndustry(enterprise.getEnterpriseIndustry());
        job.setJobCompanyLogo(enterprise.getEnterpriseLogoUrl());


        //2.保存岗位信息
        int insert = jobMapper.insert(job);

        //3.返回
        return (long) insert;
    }

    @Override
    public PageResponse<JobVO>pageJob(PageJobRequest pageJobRequest) {
        //1.获取参数
        String jobLocation = pageJobRequest.getJobLocation();
        String jobSalary = pageJobRequest.getJobSalary();
        String jobStatus = pageJobRequest.getJobStatus();
        Integer jobAdminStatus = pageJobRequest.getJobAdminStatus();
        String jobIndustry = pageJobRequest.getJobIndustry();
        Long jobCompanyId = pageJobRequest.getJobCompanyId();
        String searchName = pageJobRequest.getSearchName();

        //2.构建分页条件
        Page<Job> page = pageJobRequest.toPageDefaultSortByUpdateTime();

        //3.分页查询
        Page<Job> jobPage = lambdaQuery()
                .eq(StrUtil.isNotBlank(jobStatus), Job::getJobStatus, jobStatus)
                .eq(jobAdminStatus != null, Job::getJobAdminStatus, jobAdminStatus)
                .like(StrUtil.isNotBlank(jobIndustry), Job::getJobIndustry, jobIndustry)
                .like(StrUtil.isNotBlank(jobLocation), Job::getJobLocation, jobLocation)
                .like(StrUtil.isNotBlank(jobSalary), Job::getJobSalary, jobSalary)
                .eq(jobCompanyId != null, Job::getJobCompanyId, jobCompanyId)
                .and(StrUtil.isNotBlank(searchName),
                        w -> w.like( Job::getJobTitle, searchName)
                                .or()
                                .like(Job::getJobCompanyName, searchName)
                                .or()
                                .like(Job::getJobSalary, searchName)
                                .or()
                                .like(Job::getJobLocation, searchName))
                .page(page);
        return PageResponse.of(jobPage, JobVO.class);
    }

    @Override
    public boolean updateJobById(UpdateJobRequest updateJobRequest) {

        //1.获取参数
        Long jobId = updateJobRequest.getJobId();
        String jobTitle = updateJobRequest.getJobTitle();
        String jobLocation = updateJobRequest.getJobLocation();
        Long jobSalary = updateJobRequest.getJobSalary();
        String jobEducationRequirement = updateJobRequest.getJobEducationRequirement();
        String jobMajorRequirement = updateJobRequest.getJobMajorRequirement();
        String jobExperienceRequirement = updateJobRequest.getJobExperienceRequirement();
        String jobSkillRequirement = updateJobRequest.getJobSkillRequirement();
        String jobDescription = updateJobRequest.getJobDescription();
        String jobBenefits = updateJobRequest.getJobBenefits();
        Date jobPublishDate = updateJobRequest.getJobPublishDate();
        Date jobDeadline = updateJobRequest.getJobDeadline();
        String jobContactInfo = updateJobRequest.getJobContactInfo();
        String jobStatus = updateJobRequest.getJobStatus();
        String jobIndustry = updateJobRequest.getJobIndustry();

        //2.更新岗位信息
        LambdaUpdateWrapper<Job> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Job::getJobId, jobId)
                .set(StrUtil.isNotBlank(jobTitle), Job::getJobTitle, jobTitle)
                .set(StrUtil.isNotBlank(jobLocation), Job::getJobLocation, jobLocation)
                .set(jobSalary != null, Job::getJobSalary, jobSalary)
                .set(StrUtil.isNotBlank(jobEducationRequirement), Job::getJobEducationRequirement, jobEducationRequirement)
                .set(StrUtil.isNotBlank(jobMajorRequirement), Job::getJobMajorRequirement, jobMajorRequirement)
                .set(StrUtil.isNotBlank(jobExperienceRequirement), Job::getJobExperienceRequirement, jobExperienceRequirement)
                .set(StrUtil.isNotBlank(jobSkillRequirement), Job::getJobSkillRequirement, jobSkillRequirement)
                .set(StrUtil.isNotBlank(jobDescription), Job::getJobDescription, jobDescription)
                .set(StrUtil.isNotBlank(jobBenefits), Job::getJobBenefits, jobBenefits)
                .set(jobPublishDate != null, Job::getJobPublishDate, jobPublishDate)
                .set(jobDeadline != null, Job::getJobDeadline, jobDeadline)
                .set(StrUtil.isNotBlank(jobContactInfo), Job::getJobContactInfo, jobContactInfo)
                .set(StrUtil.isNotBlank(jobStatus), Job::getJobStatus, jobStatus)
                .set(StrUtil.isNotBlank(jobIndustry), Job::getJobIndustry, jobIndustry);
        return jobMapper.update(null, updateWrapper)>0;
    }

    @Override
    public IndustryJobSpread querySpreadJobCount() {

        IndustryJobSpread spreadJob = new IndustryJobSpread();
        //岗位分布数量
        //1.互联网
        QueryWrapper<Job> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("job_industry","互联网/AI");
        spreadJob.setInternetCount(jobMapper.selectCount(queryWrapper));
        //2.服务
        queryWrapper.clear();
        queryWrapper.eq("job_industry","服务业/餐饮");
        spreadJob.setServiceCount(jobMapper.selectCount(queryWrapper));

        //3.金融
        queryWrapper.clear();
        queryWrapper.eq("job_industry","金融/制造");
        spreadJob.setFinanceCount(jobMapper.selectCount(queryWrapper));
        //4.医疗
        queryWrapper.clear();
        queryWrapper.eq("job_industry","医疗/制药");
        spreadJob.setMedicalCount(jobMapper.selectCount(queryWrapper));
        //5.教育
        queryWrapper.clear();
        queryWrapper.eq("job_industry","教育/培训");
        spreadJob.setEducationCount(jobMapper.selectCount(queryWrapper));
        //6.其他
        queryWrapper.clear();
        queryWrapper.eq("job_industry","其他行业");
        spreadJob.setOtherCount(jobMapper.selectCount(queryWrapper));

        //7.游戏电子
        queryWrapper.clear();
        queryWrapper.eq("job_industry","游戏/电子");
        spreadJob.setOtherCount(jobMapper.selectCount(queryWrapper));

        //8.网络社交/媒体
        queryWrapper.clear();
        queryWrapper.eq("job_industry","网络社交/媒体");
        spreadJob.setOtherCount(jobMapper.selectCount(queryWrapper));

        //9.房地产/建筑
        queryWrapper.clear();
        queryWrapper.eq("job_industry","房地产/建筑");
        spreadJob.setOtherCount(jobMapper.selectCount(queryWrapper));

        //8.零售/批发
        queryWrapper.clear();
        queryWrapper.eq("job_industry","零售/批发");
        spreadJob.setOtherCount(jobMapper.selectCount(queryWrapper));

        return spreadJob;
    }

    @Override
    public CityAverageSalary queryHotCityAverageSalary() {
        return jobMapper.queryAverageSalariesByCities();
    }

    @Override
    public List<JobVO> queryHotJob() {
        //1.查询出投递最多的五个岗位
        QueryWrapper<Send> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("job_id,count(job_id) as count")
                .groupBy("job_id")
                .orderByDesc("count")
                .last("limit 5");
        List<Send> sends = sendMapper.selectList(queryWrapper);
        if(sends.size()==0){
            return null;
        }
        //2.根据岗位id查询岗位信息
        QueryWrapper<Job> jobQueryWrapper = new QueryWrapper<>();
        jobQueryWrapper.in("job_id",sends.stream().map(Send::getJobId).toList());
        List<Job> jobs = jobMapper.selectList(jobQueryWrapper);
        List<JobVO> jobVOS = jobs.stream().map(job -> {
            JobVO jobVO = BeanUtil.copyProperties(job, JobVO.class);
            return jobVO;

        }).toList();
        return jobVOS;
    }

    @Override
    public List<JobVO> queryJobByEnterpriseId(Long enterpriseId) {
        //1.根据企业id查询岗位信息
        QueryWrapper<Job> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("job_company_id",enterpriseId);
        List<Job> jobs = jobMapper.selectList(queryWrapper);
        List<JobVO> jobVOS = jobs.stream().map(job -> {
            JobVO jobVO = BeanUtil.copyProperties(job, JobVO.class);
            return jobVO;

        }).toList();
        return jobVOS;
    }

    @Override
    public List<JobVO> queryJobByJobIndustry(List<String> jobIndustry) {
        //1.获取参数
        List<JobVO> jobVos= new ArrayList<>();
        for (String industry : jobIndustry) {
            QueryWrapper<Job> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("job_industry",industry);
            queryWrapper.eq("job_status","开放");
            queryWrapper.eq("job_admin_status",0);
            List<Job> jobs = jobMapper.selectList(queryWrapper);
            jobs.forEach(job -> {
                JobVO jobVO = BeanUtil.copyProperties(job, JobVO.class);
                jobVos.add(jobVO);
            });
        }
        return jobVos;
    }

    @Override
    public List<JobVO> queryJobByJobPosition(List<String> jobPosition) {
        //1.获取参数
        List<JobVO> jobVos= new ArrayList<>();
        for (String position : jobPosition) {
            QueryWrapper<Job> queryWrapper = new QueryWrapper<>();
            queryWrapper.like("job_title",position);
            queryWrapper.eq("job_status","开放");
            queryWrapper.eq("job_admin_status",0);
            List<Job> jobs = jobMapper.selectList(queryWrapper);
            jobs.forEach(job -> {
                JobVO jobVO = BeanUtil.copyProperties(job, JobVO.class);
                jobVos.add(jobVO);
            });
        }
        return jobVos;
    }

    @Override
    public List<String> queryJobTitleByEnterpriseId(Long enterpriseId) {
        //1.根据企业id查询岗位信息
        QueryWrapper<Job> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("job_company_id",enterpriseId);
        queryWrapper.eq("job_admin_status",0);
        queryWrapper.eq("job_status","开放");
        List<Job> jobs = jobMapper.selectList(queryWrapper);
        //2.获取岗位名称
        return jobs.stream().map(Job::getJobTitle).toList();
    }

    @Override
    public List<String> queryJobIndustryByEnterpriseId(Long enterpriseId) {
        //1.根据企业id查询岗位信息
        QueryWrapper<Job> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("job_company_id",enterpriseId);
        queryWrapper.eq("job_admin_status",0);
        queryWrapper.eq("job_status","开放");
        List<Job> jobs = jobMapper.selectList(queryWrapper);
        //2.获取岗位所属行业
        return jobs.stream().map(Job::getJobIndustry).toList();
    }
}




